package com.zd.kids.net;


import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;

import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.hyy.libray.common.Constant;
import com.hyy.libray.common.LogUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.zd.kids.common.ToastUtils;
import com.zd.kids.ui.activity.InputLoginMobileActivity;
import com.zd.kids.ui.activity.MainTypeActivity;
import com.zd.kids.ui.activity.WelcomeActivity;

import java.net.ConnectException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public abstract class CustomObserver<T> implements Observer<T> {


    Context context;

    public CustomObserver() {
    }

    public CustomObserver(Context context) {
        this.context = context;
    }

    @Override
    public void onSubscribe(Disposable d) {

        if (!NetworkUtils.isConnected()) {

            if (context != null) {
                new XPopup.Builder(context)
                        .dismissOnBackPressed(false)
                        .dismissOnTouchOutside(false)
                        .autoDismiss(true)
                        .asConfirm("連接網絡", "您的網絡連線不穩定，請確保連線至網絡，然後再試一次", new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                //打开系统设置界面
                                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                                context.startActivity(intent);
                            }
                        })
                        .hideCancelBtn()
                        .show();
            } else {
                ToastUtils.getIntence().makeTextShow("您的網絡連線不穩定，請確保連線至網絡，然後再試一次");
            }

            onError(new BusinessException("", BusinessException.NO_NETWORK + ""));
        }
    }

    @Override
    public void onNext(T t) {
        onSuccess(t);
    }


    @Override
    public void onError(Throwable e) {

        if (e instanceof BusinessException) {
            onError(((BusinessException) e).getCode());
            if (((BusinessException) e).getCode().equals("401")) {
                SPUtils.getInstance(Constant.GAPSK_AI_QM).put("jwt", "");
                if (context != null) {
                    Intent intent = new Intent(context, InputLoginMobileActivity.class);
                    intent.putExtra("PswError", true);
                    context.startActivity(intent);
                }
            }
            String tips = ((BusinessException) e).getMsg();

            if (!TextUtils.isEmpty(tips)) {
                ToastUtils.getIntence().makeTextShow(tips);
            }

        } else {
            if (!NetworkUtils.isConnected()) {
                LogUtils.debugInfo("isConnected", "=====isConnected=====");
            } else if (e instanceof ConnectException) {
                ToastUtils.getIntence().makeTextShow("網絡異常，請聯系工作人員");
            } else if (e instanceof HttpException) {
                LogUtils.debugInfo("isConnected", "=====HttpException=====");
                String msg = ((HttpException) e).message();
                if (!TextUtils.isEmpty(msg)) {
                    ToastUtils.getIntence().makeTextShow(msg);
                }
            } else {
                LogUtils.debugInfo("isConnected", "=====makeTextShow=====");
                ToastUtils.getIntence().makeTextShow(e.getMessage());
            }
            onError(e.getMessage());
        }

    }

    @Override
    public void onComplete() {

    }

    public abstract void onSuccess(T t);

    public abstract void onError(String errorCode);
}
